set more off

use dataset, clear

local Z pcteligible2013

su `Z', det
gen Z = `Z' >=r(p50)
replace Z = . if mi(`Z')
 
gen Zx = Z*x
gen Zz = Z*z
gen Zzx = Z*zx

foreach x in z Z Zz {
	gen `x'B = ""
	gen `x'CIL = ""
	gen `x'CIU = ""	
	gen `x'BW = ""	
}
foreach x in z Z Zz {
	gen `x'N = ""
	gen `x'Ns = ""
	gen `x'bw = ""
	gen `x'cov = ""
	gen `x'rsq = ""
}

gen index = _n

* programs to store estimates (eststo doesn't work well with cgmboot)
capture program drop myest1
program define myest1
	args i bw
	mat bootresults = e(bootresults)
	svmat bootresults
	local j = 4
	foreach x in z Z Zz {
		replace `x'B = string(_b[`x'], "%20.02fc") if index == `i'
		_pctile bootresults`j' if bootresults3 == 0, nq(1000)
		replace `x'CIL = "[" + string(r(r25), "%20.02fc") + "," if index == `i'
		replace `x'CIU = string(r(r975), "%20.02fc") + "]" if index == `i'
		replace `x'BW = "`bw'" if index == `i'
		if `j' == 4 {
			replace `x'N = string(e(N), "%20.00fc") if index == `i'
			replace `x'Ns = string(e(N_fipsstate), "%20.00fc") if index == `i'
			replace `x'rsq = string(e(r2), "%20.02fc") if index == `i'
		}
		local j = `j' + 1
	}	
	drop bootresults*
end

*** do the regressions ***

local nreps 500
local windows 600 400 200 100 50 10 

local covariatesi pctwhite pct65plus pcths lmedianincome logvap2014 pctui2013
local covariates20142010r pctwhite pct65plus pcths lmedianincome logvap2014 demvoteshare2012 registration2010 swing2012 Gub2010 Sen2010 Gub2010Sen2010 Gub2014 Sen2014 Gub2014Sen2014
local covariates20162012r pctwhite pct65plus pcths lmedianincome logvap2016 demvoteshare2012 registration2012 swing2012
local covariates20142010t pctwhite pct65plus pcths lmedianincome logvap2014 demvoteshare2012 turnout2010 swing2012 Gub2010 Sen2010 Gub2010Sen2010 Gub2014 Sen2014 Gub2014Sen2014
local covariates20162012t pctwhite pct65plus pcths lmedianincome logvap2016 demvoteshare2012 turnout2012 swing2012

foreach dv in dpctui20142013 dpctui20152013 dregistration20142010 dregistration20162012 dturnout20142010 dturnout20162012 {

	preserve

		local j = 1
		local mytitle = subinstr("`dv'", "dturnout", "Turnout ", 1)
		local mytitle = subinstr("`mytitle'", "dregistration", "Registration ", 1)
		local mytitle = subinstr("`mytitle'", "2016", " 2016-", 1)
		local mytitle = subinstr("`mytitle'", "2014", " 2014-", 1)
		local mytitle = subinstr("`mytitle'", "  ", " ", 1)
		if "`dv'" == "dpctui20142013" local mytitle "Uninsured 2014-2013"
		if "`dv'" == "dpctui20152013" local mytitle "Uninsured 2015-2013"
					
		if regexm("`dv'", "pctui") local covariates `covariatesi'
		if "`dv'" == "dregistration20142010" local covariates `covariates20142010r'
		if "`dv'" == "dturnout20142010" local covariates `covariates20142010t'
		if "`dv'" == "dregistration20162012" local covariates `covariates20162012r'
		if "`dv'" == "dturnout20162012" local covariates `covariates20162012t'
		
		foreach c in `windows' { 
			cgmwildboot `dv' z Z Zz x zx Zx Zzx `covariates'  if abs(x) < `c', cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
			myest1 `j' `c'
			local j = `j' + 1
		}

		destring ZzB ZzBW ZzCI*, replace ignore("]" "[]" ",")
					
		#delimit;

		gr tw 
			(sc ZzB ZzBW, col(black))
			(rspike ZzCIL ZzCIU ZzBW, col(black black))
			,
				legend(off)
				xscale(reverse)
				xtitle("Window")
				plotregion(style(none))
				yline(0, lpat(dash))
				ylab(`myrange', angle(horiz))
				ytitle("Effect estimate")
				xlab(`windows', labsize(small))
				name(g`dv', replace)
				title(`mytitle')
				subtitle(`mysubti')
				
				;
				
		#delimit cr
	
	restore

}

gr combine gdpctui20142013 gdpctui20152013 gdregistration20142010 gdturnout20142010 gdregistration20162012 gdturnout20162012, cols(2) ysize(3)

gr export "_output/figureA1.pdf", replace
gr close
